package com.xiaoc.bulldozer.common.service.impl;

import com.xc.framework.common.exception.BusinessException;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dao.AccountBalanceMapper;
import com.xiaoc.bulldozer.common.dto.AccountBalanceQueryDto;
import com.xiaoc.bulldozer.common.model.AccountBalance;
import com.xiaoc.bulldozer.common.service.AccountBalanceService;
import com.xiaoc.bulldozer.common.util.LogUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * AccountBalance Service 实现类
 *
 * @author Danfo Yam
 * @date 2018-08-24 15:47:58
 */
@Service("accountBalanceService")
public class AccountBalanceServiceImpl implements AccountBalanceService {

    @Resource
    private AccountBalanceMapper accountBalanceMapper;

    @Override
    public Integer getAccountBalanceCountByCondition(AccountBalanceQueryDto accountBalanceQueryDto) {
        return accountBalanceMapper.getAccountBalanceCountByCondition(accountBalanceQueryDto);
    }

    @Override
    public List<AccountBalance> getAccountBalanceListByCondition(AccountBalanceQueryDto accountBalanceQueryDto) {
        accountBalanceQueryDto.setPage(accountBalanceQueryDto.getPage());
        return accountBalanceMapper.getAccountBalanceListByCondition(accountBalanceQueryDto);
    }

    @Override
    public AccountBalance getAccountBalanceById(Integer id) {
        return accountBalanceMapper.getAccountBalanceById(id);
    }

    @Override
    public List<AccountBalance> getAllAccountBalanceList() {
        return accountBalanceMapper.getAllAccountBalanceList();
    }

    @Override
    public void save(AccountBalance accountBalance) {
        accountBalanceMapper.save(accountBalance);
    }

    @Override
    public void update(AccountBalance accountBalance) {
        accountBalanceMapper.update(accountBalance);
    }

    @Override
    public void deleteById(Integer id) {
        accountBalanceMapper.deleteById(id);
    }

    @Override
    public AccountBalance getAccountBalanceAccountIdAndType(Integer accountId, Integer type) {
        return accountBalanceMapper.getAccountBalanceAccountIdAndType(accountId, type);
    }

    @Override
    public void updateBalanceAndUpdateTimeById(Integer balance, long updateTime, Integer id) {
        accountBalanceMapper.updateBalanceAndUpdateTimeById(balance, updateTime, id);
    }

    @Override
    public List<AccountBalance> getAccountBalanceListByUserId(Integer userId) {
        return accountBalanceMapper.getAccountBalanceListByUserId(userId);
    }

    @Override
    public AccountBalance getAccountBalanceByUserIdAndType(Integer userId, Integer type) {
        return accountBalanceMapper.getAccountBalanceByUserIdAndType(userId, type);
    }

    ;

    @Override
    public AccountBalance saveAccountBalance(Integer userId, int fee, int balanceType, int balanceDetailType) {

        AccountBalance accountBalance = this.getAccountBalanceByUserIdAndType(userId, balanceType);

        long currentTime = System.currentTimeMillis();

        if (accountBalance == null) {
            throw new BusinessException(ResponseCode.BUSINESS_ERROR.getCode(), ResponseCode.BUSINESS_ERROR.getMsg());
//            accountBalance = new AccountBalance();
//            accountBalance.setBalance(fee);
//            //accountBalance.setAccountId(accountId);
//            accountBalance.setUserId(userId);
//            accountBalance.setCreateTime(currentTime);
//            accountBalance.setUpdateTime(currentTime);
//            this.save(accountBalance);
//            return accountBalance;
        }

        if (fee == 0) {
            return accountBalance;
        }

        int balance = accountBalance.getBalance();

        accountBalanceMapper.changeBalance(accountBalance.getId(), fee, currentTime);

        accountBalance = this.getAccountBalanceByUserIdAndType(userId, balanceType);

        LogUtils.logBalance(userId, balance, fee, accountBalance.getBalance(), balanceType, balanceDetailType);

        return accountBalance;
    }
}
